﻿unit SysRightRegisterModel;

interface

uses
  FastMM4,
  FastMove,
  System.SysUtils,
  System.Classes,
  System.Types,
  Winapi.Windows,
  Data.DB,
  Data.Win.ADODB,
  Datasnap.DBClient,
  Datasnap.Provider,
  {$IFDEF UNICODE}
  AnsiStrings,
  {$ENDIF }
  QBParcel,
  DllSpread,
  Generics.Collections,
  DateUtils,
  QJson,
  UserConnection,
  DBAccessor,
  DllPlugin,
  StrUtils,
  SKSOFTComm;

type

  TSysRightRegisterModel = class(TObject)
  public 
    const cModuleKey='SysRightRegister'; 
    const cTblName ='sys_right_register';
    const cBaseView = 'view_sys_right_register';
    const cObjNameKey='ObjName';
    const cObjGUIDKey='ObjGUID';
    const cObjItemsKey='Items';
    const cCdsModelKey='CdsModel';
    
    class function GetCountSQL(const iCondition:string; const dLoadMode:eDBLoadMode=eDBLoadMode.dblmTable):string;
    class function GetQuerySQL(const iCondition:string; const dOrderBy:string=''; const dLoadMode:eDBLoadMode=eDBLoadMode.dblmTable):string;
    class function GetPageQuerySQL(const iPageNo,iPageSize:Integer; const iCondition,iOrderBy:string; const dLoadMode:eDBLoadMode=eDBLoadMode.dblmTable):string;

    const cRightIdKey='right_id';
    const cRightTreeIdKey='right_tree_id';
    const cRightTreePidKey='right_tree_pid';
    const cRightNameKey='right_name';
    const cRightDeepKey='right_deep';
    const cRightStatusKey='right_status';
    const cRightSortNoKey='right_sort_no';
    const cRightIsSystemKey='right_is_system';
    const cRightIsUseInfoDomainKey='right_is_use_info_domain';
    const cRightUiManageIdKey='right_ui_manage_id';
    const cRightControlStrKey='right_control_str';
    const cRightIconStrKey='right_icon_str';
    const cRightRemarkKey='right_remark';
    const cRecordPsnKey='record_psn';
    const cRecordDateKey='record_date';
    const cModifyPsnKey='modify_psn';
    const cModifyDateKey='modify_date';
    const cTimeStampKey='time_stamp';
  end;
  
  TSysRightRegister = class(TObject)
  private    
    fRightId:string;
    fRightTreeId:string;
    fRightTreePid:string;
    fRightName:string;
    fRightDeep:Integer;
    fRightStatus:Integer;
    fRightSortNo:Integer;
    fRightIsSystem:Boolean;
    fRightIsUseInfoDomain:Boolean;
    fRightUiManageId:string;
    fRightControlStr:string;
    fRightIconStr:string;
    fRightRemark:string;
    fRecordPsn:string;
    fRecordDate:TDateTime;
    fModifyPsn:string;
    fModifyDate:TDateTime;
    fTimeStamp:Int64;
    
  protected
    fObjGUID:string;
    fInitDate:TDateTime;
    fCdsModel:TClientDataSet;
    function ObjectToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    
  public
    constructor Create();overload;
    destructor Destroy;override;
    
    procedure ResetObject;
    
    function LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
    function SaveToStr():string;
    
    property ObjGUID:string read fObjGUID;
    property RightId:string read fRightId write fRightId;
    property RightTreeId:string read fRightTreeId write fRightTreeId;
    property RightTreePid:string read fRightTreePid write fRightTreePid;
    property RightName:string read fRightName write fRightName;
    property RightDeep:Integer read fRightDeep write fRightDeep;
    property RightStatus:Integer read fRightStatus write fRightStatus;
    property RightSortNo:Integer read fRightSortNo write fRightSortNo;
    property RightIsSystem:Boolean read fRightIsSystem write fRightIsSystem;
    property RightIsUseInfoDomain:Boolean read fRightIsUseInfoDomain write fRightIsUseInfoDomain;
    property RightUiManageId:string read fRightUiManageId write fRightUiManageId;
    property RightControlStr:string read fRightControlStr write fRightControlStr;
    property RightIconStr:string read fRightIconStr write fRightIconStr;
    property RightRemark:string read fRightRemark write fRightRemark;
    property RecordPsn:string read fRecordPsn write fRecordPsn;
    property RecordDate:TDateTime read fRecordDate write fRecordDate;
    property ModifyPsn:string read fModifyPsn write fModifyPsn;
    property ModifyDate:TDateTime read fModifyDate write fModifyDate;
    property TimeStamp:Int64 read fTimeStamp write fTimeStamp;
  end;

  TSysRightRegisterList = class(TDictionary<string,TSysRightRegister>)
  private
    fCdsModel:TClientDataSet;
    fSortKeys:TStrings;
    
  public
    constructor Create();overload;
    destructor Destroy;override;
    
    procedure ResetObject;
    
    function LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
    function SaveToStr():string;
    
    function FindByKey(const iKey:string):TSysRightRegister;
    function IsExist(iKey:string):Boolean;
    function Add(const iItem:TSysRightRegister):string;
    function Delete(const iKey:string):Boolean;
    function Remove(const iKey:string):TSysRightRegister;
    procedure Clear;
    
    property SortKeys:TStrings read fSortKeys write fSortKeys;
  end;

implementation

class function TSysRightRegisterModel.GetCountSQL(const iCondition:string; const dLoadMode:eDBLoadMode):string;
var
  ReadName:string;
begin
  case dLoadMode of
    dblmNone: ReadName:=TSysRightRegisterModel.cTblName;
    dblmTable: ReadName:=TSysRightRegisterModel.cTblName;
    dblmView: ReadName:=TSysRightRegisterModel.cBaseView;
  end;
  
  if SameStr(Trim(iCondition),EmptyStr) then
  begin
    Result:=Format(string('SELECT COUNT(*) AS ''COUNT'' FROM %s;'),[ReadName]);
  end else begin
    Result:=Format(string('SELECT COUNT(*) AS ''COUNT'' FROM %s WHERE %s;'),[ReadName,iCondition]);
  end;
end;

class function TSysRightRegisterModel.GetQuerySQL(const iCondition:string; const dOrderBy:string; const dLoadMode:eDBLoadMode):string;
var
  ReadName:string;
begin
  case dLoadMode of
    dblmNone: ReadName:=TSysRightRegisterModel.cTblName;
    dblmTable: ReadName:=TSysRightRegisterModel.cTblName;
    dblmView: ReadName:=TSysRightRegisterModel.cBaseView;
  end;
  
  if SameStr(Trim(iCondition),EmptyStr) then
  begin
    Result:=Format(string('SELECT * FROM %s %s;'),[ReadName,
                                                 IfThen(SameStr(dOrderBy,EmptyStr),'','ORDER BY '+dOrderBy)]);
  end else begin
    Result:=Format(string('SELECT * FROM %s WHERE %s %s;'),[ReadName,iCondition,
                                                            IfThen(SameStr(dOrderBy,EmptyStr),'','ORDER BY '+dOrderBy)]);
  end;
end;

class function TSysRightRegisterModel.GetPageQuerySQL(const iPageNo,iPageSize:Integer; const iCondition,iOrderBy:string; const dLoadMode:eDBLoadMode):string;
var
  ReadName:string;
begin
  case dLoadMode of
    dblmNone: ReadName:=TSysRightRegisterModel.cTblName;
    dblmTable: ReadName:=TSysRightRegisterModel.cTblName;
    dblmView: ReadName:=TSysRightRegisterModel.cBaseView;
  end;
  
  if SameStr(Trim(iCondition),EmptyStr) then
  begin      
    Result:=Format(string('SELECT * FROM %s'),[ReadName]);
  end else begin
    Result:=Format(string('SELECT * FROM %s WHERE %s'),[ReadName,iCondition]);
  end;
  Result:=Format(string('SELECT TOP %d * FROM (SELECT row_number() OVER(ORDER BY %s) AS _row_no_,* FROM (%s) AS A) AS B WHERE _row_no_>%d ORDER BY _row_no_'),
                  [iPageSize,
                  IfThen(SameStr(Trim(iOrderBy),EmptyStr),'',iOrderBy),
                  Result,
                  (iPageNo-1)*iPageSize]);
end;

constructor TSysRightRegister.Create();
begin
  inherited Create;
  fInitDate:=EncodeDateTime( 1899, 12, 30, 0, 0, 0, 0 );
  fCdsModel:=TClientDataSet.Create(nil);
  ResetObject;
end;

destructor TSysRightRegister.Destroy;
begin
  FreeAndNil(fCdsModel);
  inherited Destroy;
end;

procedure TSysRightRegister.ResetObject;
begin
  fObjGUID:=TSKSOFTComm.GetGUID;
  fRightId:=EmptyStr;
  fRightTreeId:=EmptyStr;
  fRightTreePid:=EmptyStr;
  fRightName:=EmptyStr;
  fRightDeep:=-1;
  fRightStatus:=-1;
  fRightSortNo:=-1;
  fRightIsSystem:=False;
  fRightIsUseInfoDomain:=False;
  fRightUiManageId:=EmptyStr;
  fRightControlStr:=EmptyStr;
  fRightIconStr:=EmptyStr;
  fRightRemark:=EmptyStr;
  fRecordPsn:=EmptyStr;
  fRecordDate:=fInitDate;
  fModifyPsn:=EmptyStr;
  fModifyDate:=fInitDate;
  fTimeStamp:=-1;
end;

function TSysRightRegister.LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
begin
  if (iCds<>nil) then
  begin
    ResetObject;
    if (iCds.FindField(TSysRightRegisterModel.cRightIdKey)<>nil) then
    begin
      fRightId:=iCds.FieldByName(TSysRightRegisterModel.cRightIdKey).AsString;     
      fObjGUID:=TSKSOFTComm.SetObjGUID(fRightId);
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cRightIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cRightTreeIdKey)<>nil) then
    begin
      fRightTreeId:=iCds.FieldByName(TSysRightRegisterModel.cRightTreeIdKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cRightTreeIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cRightTreePidKey)<>nil) then
    begin
      fRightTreePid:=iCds.FieldByName(TSysRightRegisterModel.cRightTreePidKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cRightTreePidKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cRightNameKey)<>nil) then
    begin
      fRightName:=iCds.FieldByName(TSysRightRegisterModel.cRightNameKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cRightNameKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cRightDeepKey)<>nil) then
    begin
      fRightDeep:=iCds.FieldByName(TSysRightRegisterModel.cRightDeepKey).AsInteger;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cRightDeepKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cRightStatusKey)<>nil) then
    begin
      fRightStatus:=iCds.FieldByName(TSysRightRegisterModel.cRightStatusKey).AsInteger;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cRightStatusKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cRightSortNoKey)<>nil) then
    begin
      fRightSortNo:=iCds.FieldByName(TSysRightRegisterModel.cRightSortNoKey).AsInteger;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cRightSortNoKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cRightIsSystemKey)<>nil) then
    begin
      fRightIsSystem:=iCds.FieldByName(TSysRightRegisterModel.cRightIsSystemKey).AsBoolean;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cRightIsSystemKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cRightIsUseInfoDomainKey)<>nil) then
    begin
      fRightIsUseInfoDomain:=iCds.FieldByName(TSysRightRegisterModel.cRightIsUseInfoDomainKey).AsBoolean;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cRightIsUseInfoDomainKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cRightUiManageIdKey)<>nil) then
    begin
      fRightUiManageId:=iCds.FieldByName(TSysRightRegisterModel.cRightUiManageIdKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cRightUiManageIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cRightControlStrKey)<>nil) then
    begin
      fRightControlStr:=iCds.FieldByName(TSysRightRegisterModel.cRightControlStrKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cRightControlStrKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cRightIconStrKey)<>nil) then
    begin
      fRightIconStr:=iCds.FieldByName(TSysRightRegisterModel.cRightIconStrKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cRightIconStrKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cRightRemarkKey)<>nil) then
    begin
      fRightRemark:=iCds.FieldByName(TSysRightRegisterModel.cRightRemarkKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cRightRemarkKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cRecordPsnKey)<>nil) then
    begin
      fRecordPsn:=iCds.FieldByName(TSysRightRegisterModel.cRecordPsnKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cRecordPsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cRecordDateKey)<>nil) then
    begin
      if iCds.FindField(TSysRightRegisterModel.cRecordDateKey).IsNull then
      begin
        fRecordDate:=fInitDate;
      end else begin
        fRecordDate:=iCds.FieldByName(TSysRightRegisterModel.cRecordDateKey).AsDateTime;
      end;    
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cRecordDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cModifyPsnKey)<>nil) then
    begin
      fModifyPsn:=iCds.FieldByName(TSysRightRegisterModel.cModifyPsnKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cModifyPsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cModifyDateKey)<>nil) then
    begin
      if iCds.FindField(TSysRightRegisterModel.cModifyDateKey).IsNull then
      begin
        fModifyDate:=fInitDate;
      end else begin
        fModifyDate:=iCds.FieldByName(TSysRightRegisterModel.cModifyDateKey).AsDateTime;
      end;    
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cModifyDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cTimeStampKey)<>nil) then
    begin
      fTimeStamp:=iCds.FieldByName(TSysRightRegisterModel.cTimeStampKey).AsLargeInt;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cTimeStampKey]);
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.LoadFromCDS函数参数错误!错误内容:传入的Cds对象为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSysRightRegister.ObjectToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
begin
  if (iCds<>nil) then
  begin
    if (iCds.FindField(TSysRightRegisterModel.cRightIdKey)<>nil) then
    begin
      iCds.FieldByName(TSysRightRegisterModel.cRightIdKey).AsString:=fRightId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cRightIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cRightTreeIdKey)<>nil) then
    begin
      iCds.FieldByName(TSysRightRegisterModel.cRightTreeIdKey).AsString:=fRightTreeId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cRightTreeIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cRightTreePidKey)<>nil) then
    begin
      iCds.FieldByName(TSysRightRegisterModel.cRightTreePidKey).AsString:=fRightTreePid;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cRightTreePidKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cRightNameKey)<>nil) then
    begin
      iCds.FieldByName(TSysRightRegisterModel.cRightNameKey).AsString:=fRightName;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cRightNameKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cRightDeepKey)<>nil) then
    begin
      iCds.FieldByName(TSysRightRegisterModel.cRightDeepKey).AsInteger:=fRightDeep;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cRightDeepKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cRightStatusKey)<>nil) then
    begin
      iCds.FieldByName(TSysRightRegisterModel.cRightStatusKey).AsInteger:=fRightStatus;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cRightStatusKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cRightSortNoKey)<>nil) then
    begin
      iCds.FieldByName(TSysRightRegisterModel.cRightSortNoKey).AsInteger:=fRightSortNo;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cRightSortNoKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cRightIsSystemKey)<>nil) then
    begin
      iCds.FieldByName(TSysRightRegisterModel.cRightIsSystemKey).AsBoolean:=fRightIsSystem;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cRightIsSystemKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cRightIsUseInfoDomainKey)<>nil) then
    begin
      iCds.FieldByName(TSysRightRegisterModel.cRightIsUseInfoDomainKey).AsBoolean:=fRightIsUseInfoDomain;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cRightIsUseInfoDomainKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cRightUiManageIdKey)<>nil) then
    begin
      iCds.FieldByName(TSysRightRegisterModel.cRightUiManageIdKey).AsString:=fRightUiManageId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cRightUiManageIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cRightControlStrKey)<>nil) then
    begin
      iCds.FieldByName(TSysRightRegisterModel.cRightControlStrKey).AsString:=fRightControlStr;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cRightControlStrKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cRightIconStrKey)<>nil) then
    begin
      iCds.FieldByName(TSysRightRegisterModel.cRightIconStrKey).AsString:=fRightIconStr;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cRightIconStrKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cRightRemarkKey)<>nil) then
    begin
      iCds.FieldByName(TSysRightRegisterModel.cRightRemarkKey).AsString:=fRightRemark;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cRightRemarkKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cRecordPsnKey)<>nil) then
    begin
      iCds.FieldByName(TSysRightRegisterModel.cRecordPsnKey).AsString:=fRecordPsn;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cRecordPsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cRecordDateKey)<>nil) then
    begin
      if SameDateTime(fRecordDate,fInitDate) then
      begin
        iCds.FieldByName(TSysRightRegisterModel.cRecordDateKey).AsString:=EmptyStr;
      end else begin
        iCds.FieldByName(TSysRightRegisterModel.cRecordDateKey).AsDateTime:=fRecordDate;
      end;    
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cRecordDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cModifyPsnKey)<>nil) then
    begin
      iCds.FieldByName(TSysRightRegisterModel.cModifyPsnKey).AsString:=fModifyPsn;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cModifyPsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cModifyDateKey)<>nil) then
    begin
      if SameDateTime(fModifyDate,fInitDate) then
      begin
        iCds.FieldByName(TSysRightRegisterModel.cModifyDateKey).AsString:=EmptyStr;
      end else begin
        iCds.FieldByName(TSysRightRegisterModel.cModifyDateKey).AsDateTime:=fModifyDate;
      end;    
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cModifyDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSysRightRegisterModel.cTimeStampKey)<>nil) then
    begin
      iCds.FieldByName(TSysRightRegisterModel.cTimeStampKey).AsLargeInt:=fTimeStamp;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSysRightRegisterModel.cTimeStampKey]);
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.ObjectToCDS函数参数错误!错误内容:传入的Cds对象为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSysRightRegister.SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
begin
  if (iCds<>nil) then
  begin
    iCds.Append;
    if ObjectToCDS(iCds,oError) then
    begin
      iCds.Post;
    end else begin
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.SaveToCDS函数参数错误!错误内容:传入的Cds对象为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSysRightRegister.LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
var
  Json:TQJson;
  MemStream:TMemoryStream;
begin
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create;
  try
    if Json.TryParse(iJsonStr) then
    begin
      if (Json.ItemByName(TSysRightRegisterModel.cObjNameKey)<>nil) then
      begin
        if (Self.ClassName<>Json.ItemByName(TSysRightRegisterModel.cObjNameKey).AsString) then
        begin
          //oError:=Format(string('调用%s.LoadFromStr函数！错误内容:对象类名不匹配.'),[Self.ClassName]);
          //Result:=False;
          //Exit;
        end;
      end else begin
        oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性节点.'),[Self.ClassName,TSysRightRegisterModel.cObjNameKey]);
        Result:=False;
        Exit;
      end;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:解析Json失败.iJsonStr:%s'),[Self.ClassName,iJsonStr]);
      Result:=False;
      Exit;
    end;
    ResetObject;
      
    if (Json.ItemByName(TSysRightRegisterModel.cCdsModelKey)<>nil) and 
      not SameStr(Trim(Json.ItemByName(TSysRightRegisterModel.cCdsModelKey).AsString),EmptyStr) then
    begin
      TSKSOFTComm.Base64StrToMemStream(AnsiString(Json.ItemByName(TSysRightRegisterModel.cCdsModelKey).AsString),MemStream);
      if MemStream.Size>0 then
      begin
        fCdsModel.LoadFromStream(MemStream);
      end;
    end;
    
    if (Json.ItemByName(TSysRightRegisterModel.cObjGUIDKey)<>nil) and 
      not SameStr(Trim(Json.ItemByName(TSysRightRegisterModel.cObjGUIDKey).AsString),EmptyStr) then
    begin
      fObjGUID:=Json.ItemByName(TSysRightRegisterModel.cObjGUIDKey).AsString;
    end;
    
    if Json.ItemByName('RightId')<>nil then
    begin
      fRightId:=Json.ItemByName('RightId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RightId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RightTreeId')<>nil then
    begin
      fRightTreeId:=Json.ItemByName('RightTreeId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RightTreeId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RightTreePid')<>nil then
    begin
      fRightTreePid:=Json.ItemByName('RightTreePid').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RightTreePid']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RightName')<>nil then
    begin
      fRightName:=Json.ItemByName('RightName').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RightName']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RightDeep')<>nil then
    begin
      fRightDeep:=Json.ItemByName('RightDeep').AsInteger;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RightDeep']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RightStatus')<>nil then
    begin
      fRightStatus:=Json.ItemByName('RightStatus').AsInteger;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RightStatus']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RightSortNo')<>nil then
    begin
      fRightSortNo:=Json.ItemByName('RightSortNo').AsInteger;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RightSortNo']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RightIsSystem')<>nil then
    begin
      fRightIsSystem:=Json.ItemByName('RightIsSystem').AsBoolean;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RightIsSystem']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RightIsUseInfoDomain')<>nil then
    begin
      fRightIsUseInfoDomain:=Json.ItemByName('RightIsUseInfoDomain').AsBoolean;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RightIsUseInfoDomain']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RightUiManageId')<>nil then
    begin
      fRightUiManageId:=Json.ItemByName('RightUiManageId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RightUiManageId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RightControlStr')<>nil then
    begin
      fRightControlStr:=Json.ItemByName('RightControlStr').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RightControlStr']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RightIconStr')<>nil then
    begin
      fRightIconStr:=Json.ItemByName('RightIconStr').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RightIconStr']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RightRemark')<>nil then
    begin
      fRightRemark:=Json.ItemByName('RightRemark').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RightRemark']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RecordPsn')<>nil then
    begin
      fRecordPsn:=Json.ItemByName('RecordPsn').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RecordPsn']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RecordDate')<>nil then
    begin
      fRecordDate:=Json.ItemByName('RecordDate').AsDateTime;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RecordDate']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('ModifyPsn')<>nil then
    begin
      fModifyPsn:=Json.ItemByName('ModifyPsn').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'ModifyPsn']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('ModifyDate')<>nil then
    begin
      fModifyDate:=Json.ItemByName('ModifyDate').AsDateTime;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'ModifyDate']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('TimeStamp')<>nil then
    begin
      fTimeStamp:=Json.ItemByName('TimeStamp').AsInt64;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'TimeStamp']);
      Result:=False;
      Exit;  
    end;
    
  finally
    Json.Free;
    MemStream.Free;
  end;
  Result:=True;
end;

function TSysRightRegister.SaveToStr():string;
var
  Json:TQJson;
  MemStream:TMemoryStream;
begin
  Result:=EmptyStr;  
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create; 
  try
    Json.Add('ModuleKey').AsString:=TSysRightRegisterModel.cModuleKey;
    Json.Add(TSysRightRegisterModel.cObjNameKey).AsString:=Self.ClassName;
    
    fCdsModel.SaveToStream(MemStream);
    Json.Add(TSysRightRegisterModel.cCdsModelKey).AsString:=string(TSKSOFTComm.MemStreamToBase64Str(MemStream));
    
    Json.Add(TSysRightRegisterModel.cObjGUIDKey).AsString:=fObjGUID;
    Json.Add('RightId').AsString:=fRightId;
    Json.Add('RightTreeId').AsString:=fRightTreeId;
    Json.Add('RightTreePid').AsString:=fRightTreePid;
    Json.Add('RightName').AsString:=fRightName;
    Json.Add('RightDeep').AsInteger:=fRightDeep;
    Json.Add('RightStatus').AsInteger:=fRightStatus;
    Json.Add('RightSortNo').AsInteger:=fRightSortNo;
    Json.Add('RightIsSystem').AsBoolean:=fRightIsSystem;
    Json.Add('RightIsUseInfoDomain').AsBoolean:=fRightIsUseInfoDomain;
    Json.Add('RightUiManageId').AsString:=fRightUiManageId;
    Json.Add('RightControlStr').AsString:=fRightControlStr;
    Json.Add('RightIconStr').AsString:=fRightIconStr;
    Json.Add('RightRemark').AsString:=fRightRemark;
    Json.Add('RecordPsn').AsString:=fRecordPsn;
    Json.Add('RecordDate').AsDateTime:=fRecordDate;
    Json.Add('ModifyPsn').AsString:=fModifyPsn;
    Json.Add('ModifyDate').AsDateTime:=fModifyDate;
    Json.Add('TimeStamp').AsInt64:=fTimeStamp;
    Result:=Json.AsString;
  finally
    Json.Free;
    MemStream.Free;
  end;
end;

constructor TSysRightRegisterList.Create();
begin
  inherited Create;
  fCdsModel:=TClientDataSet.Create(nil);
  fSortKeys:=TStringList.Create;
  ResetObject;
end;

destructor TSysRightRegisterList.Destroy;
begin
  FreeAndNil(fCdsModel);
  Clear;
  FreeAndNil(fSortKeys);
  inherited Destroy;
end;

procedure TSysRightRegisterList.ResetObject;
begin
  Clear;
end;

function TSysRightRegisterList.LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
var
  Item:TSysRightRegister;
begin
  ResetObject;
  if iCds<>nil then
  begin
    iCds.First;
    while not iCds.Eof do
    begin
      Item:=TSysRightRegister.Create;
      if Item.LoadFromCDS(iCds,oError) then
      begin
        if Add(Item)=EmptyStr then
        begin
          Item.Free;
          Result:=False;
          Exit;
        end;
      end else begin
        Result:=False;
        Exit;
      end;
      iCds.Next;
    end;
  end else begin
    oError:=Format(string('调用%s.LoadFromCDS函数参数错误! 错误内容传入的数据集为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSysRightRegisterList.SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
var
  Key:string;
begin
  if (iCds<>nil) then
  begin
  for Key in fSortKeys do
  begin
    iCds.Append;
    if Items[Key].ObjectToCDS(iCds,oError) then
    begin
      iCds.Post;
    end else begin
      Result:=False;
      Exit;
    end;
  end;
  end else begin
    oError:=Format(string('调用%s.SaveToCDS函数参数错误! 错误内容:传入的数据集为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSysRightRegisterList.LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
var
  Json,ItemJson,ArrayJson:TQJson;
  Item:TSysRightRegister;
  MemStream:TMemoryStream;
begin
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create;
  try
    if Json.TryParse(iJsonStr) then
    begin
      if (Json.ItemByName(TSysRightRegisterModel.cObjNameKey)<>nil) then
      begin
        if (Self.ClassName<>Json.ItemByName(TSysRightRegisterModel.cObjNameKey).AsString) then
        begin
          //oError:=Format(string('调用%s.LoadFromStr函数参数错误! 错误内容:对象类名不匹配.'),[Self.ClassName]);
          //Result:=False;
          //Exit;
        end;
      end else begin
        oError:=Format(string('调用%s.LoadFromStr函数参数错误! 错误内容:缺少%s属性节点.'),[Self.ClassName,TSysRightRegisterModel.cObjNameKey]);
        Result:=False;
        Exit;
      end;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数参数错误! 错误内容:解析Json失败.iJsonStr:%s'),[Self.ClassName,iJsonStr]);
      Result:=False;
      Exit;
    end;
    if (Json.ItemByName(TSysRightRegisterModel.cObjItemsKey)<>nil) and
      (Json.ItemByName(TSysRightRegisterModel.cObjItemsKey).IsArray) then
    begin
      ResetObject;
      if Json.ItemByName(TSysRightRegisterModel.cCdsModelKey)<>nil then
      begin
        TSKSOFTComm.Base64StrToMemStream(AnsiString(Json.ItemByName(TSysRightRegisterModel.cCdsModelKey).AsString),MemStream);
        if MemStream.Size>0 then
        begin
          fCdsModel.LoadFromStream(MemStream);
        end;
      end;
      ArrayJson:=Json.ItemByName(TSysRightRegisterModel.cObjItemsKey);
      for ItemJson in ArrayJson do
      begin
        Item:=TSysRightRegister.Create;
        Item.LoadFromStr(ItemJson.AsString,oError);
        if Add(Item)=EmptyStr then
        begin
          Item.Free;
          Result:=False;
          Exit;
        end;
      end;
    end else begin
      Result:=False;
      Exit;
    end;
  finally
    Json.Free;
    MemStream.Free;
  end;
  Result:=True;
end;

function TSysRightRegisterList.SaveToStr():string;
var
  Key:string;
  Json,ItemJson,ArrayJson:TQJson;
  MemStream:TMemoryStream;
begin
  Result:=EmptyStr;
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create;
  try
    Json.Add(TSysRightRegisterModel.cObjNameKey).AsString:=Self.ClassName;
    fCdsModel.SaveToStream(MemStream);
    Json.Add(TSysRightRegisterModel.cCdsModelKey).AsString:=string(TSKSOFTComm.MemStreamToBase64Str(MemStream));

    ArrayJson:=Json.Add(TSysRightRegisterModel.cObjItemsKey,jdtArray);
    for Key in fSortKeys do
    begin
      ItemJson:=TQJson.Create;
      if ItemJson.TryParse(Items[Key].SaveToStr()) then
      begin
        ArrayJson.Add(ItemJson);
      end else begin
        ItemJson.Free;
      end;
    end;
    Result:=Json.AsString;
  finally
    Json.Free;
    MemStream.Free;
  end;
end;

function TSysRightRegisterList.FindByKey(const iKey:string):TSysRightRegister;
var
  Item:TSysRightRegister;
begin
  if TryGetValue(iKey,Item) then
  begin
    Result:=Item;
  end else begin
    Result:=nil;
  end;
end;

function TSysRightRegisterList.IsExist(iKey:string):Boolean;
var
  Item:TSysRightRegister;
begin
  Result:= TryGetValue(iKey,Item);
end;

function TSysRightRegisterList.Add(const iItem:TSysRightRegister):string;
begin
  if iItem.ObjGUID<>EmptyStr then
  begin
    inherited Add(iItem.ObjGUID,iItem);
    Result:=iItem.ObjGUID;
    fSortKeys.Add(iItem.ObjGUID);
  end else begin
    Result:=EmptyStr;
  end;
end;

function TSysRightRegisterList.Delete(const iKey:string):Boolean;
var
  Ops:Integer;
begin
  if ContainsKey(iKey) then
  begin
    Remove(iKey).Free;
    Ops:=fSortKeys.IndexOf(iKey);
    if Ops<>-1 then
    begin
      fSortKeys.Delete(Ops);
    end;
    Result:=True;
  end else begin
    Result:=False;
  end;
end;

function TSysRightRegisterList.Remove(const iKey:string):TSysRightRegister;
var
  Ops:Integer;
begin
  if ContainsKey(iKey) then
  begin
    Result:=Items[iKey];
    Ops:=fSortKeys.IndexOf(iKey);
    if Ops<>-1 then
    begin
      fSortKeys.Delete(Ops);
    end;
    inherited Remove(iKey);
  end else begin
    Result:=nil;
  end;
end;

procedure TSysRightRegisterList.Clear;
var
  Key:string;
begin
  for Key in Keys do
  begin
    if Items[Key]<>nil then
    begin
      Items[Key].Free;
    end;
  end;
  fSortKeys.Clear;
  inherited Clear;
end;

end.
